package com.authority.security.config;

import com.authority.security.filter.CheckTokenFilter;
import com.authority.security.handler.AnonymousAuthenticationHandler;
import com.authority.security.handler.CustomerAccessDeniedHandler;
import com.authority.security.handler.LoginFailureHandler;
import com.authority.security.handler.LoginSuccessHandler;
import com.authority.security.service.AuthenticationUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

@Configuration
@EnableWebSecurity
public class springSecurityConfig extends WebSecurityConfigurerAdapter {
    //认证成功处理器
    @Resource
    private LoginSuccessHandler loginSuccessHandler;
    //认证失败处理器
    @Resource
    private LoginFailureHandler loginFailureHandler;
    //匿名用户访问无权限资源时处理器
    @Resource
    private AnonymousAuthenticationHandler anonymousAuthenticationHandler;
    //认证用户访问无权限资源时处理器
    @Resource
    private CustomerAccessDeniedHandler customerAccessDeniedHandler;
    //用户认证处理器类
    @Resource
    private AuthenticationUserDetailsService authenticationUserDetailsService;
    //验证token过滤器
    @Resource
    private CheckTokenFilter checkTokenFilter;
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }
    /**
     * 处理登录认证
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //登录之前
        http.addFilterBefore(checkTokenFilter, UsernamePasswordAuthenticationFilter.class);
       //登录过程处理
        http.formLogin()//表单登录
                .loginProcessingUrl("/api/user/login")//登录请求的url
                .successHandler(loginSuccessHandler)//认证成功处理器
                .failureHandler(loginFailureHandler)//认证失败处理器
                .and()
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)//禁用session
                .and()
                .authorizeRequests()//设置需要拦截的请求
                .antMatchers("/api/user/login").permitAll()//登录请求放行
                .anyRequest().authenticated()//其他请求一律都需要进行身份验证
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(anonymousAuthenticationHandler)  //匿名无权限访问
                .accessDeniedHandler(customerAccessDeniedHandler)       //认证用户无权限访问
                .and()
                .cors();          //支持跨域请求
    }

    /**
     * 配置认证处理器
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(authenticationUserDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }
}
